package com.uh.calculation.util;

import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.microservice.core.dao.JdbcDao;

import java.util.List;
import java.util.Map;

public class GetFileds {


    private JdbcDao jdbcDao;


    private String  sql ="select  ind.*  from  `indi_dic` ind \n" +
            "right JOIN (select  *  from  `indi_dic` indi where  `column` = '%s'  and   view_name = '%s'  and year = 2021) a\n" +
            "on  ind.name_ch=a.name_ch\n" +
            "where  ind.year = %s    and   ind.view_name = '%s'";

    public GetFileds(JdbcDao jdbcDao, List<OperatorVal> list, WarnJiSuanBean warnJiSuanBean){
        this.jdbcDao=jdbcDao;
        update(list ,  warnJiSuanBean);
    }


    public void  update(List<OperatorVal> list , WarnJiSuanBean warnJiSuanBean){
        Integer  year=warnJiSuanBean.getYear();
        if(year!=2021){
            for(OperatorVal o : list) {
                String field = o .getField();
                String fs [] = field.split(";");
                String fval="";
                for(String f:fs){
                    String s[] = f.split("_");
                    String t = s[0];
                    String mf = s[1];
                    String q =String.format(sql, mf,t,year,t);
                    List<Map<String, Object>> ls =jdbcDao.loadList(q);
                    if (ls.size()>0){
                        Map<String, Object> m = ls.get(0);
                        String tup = m.get("view_name").toString();
                        String mfup = m.get("column").toString();
                        fval+=tup+"_"+mfup+";";
                    }
                }
                o.setField(fval);
            }
        }
    }
}
